<?php

require CAMINHO_SERVIDOR.'conexao.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of rodadaDAO
 *
 * @author Fernando
 */
class rodadaDAO {

    private $con;

    function __construct() {
        self::setCon(conectar());
        mysql_query('SET CHARACTER SET utf8');
    }

    public function getCon() {
        return $this->con;
    }

    public function setCon($con) {
        $this->con = $con;
    }

    public function fecharConexao() {
        desconectar(self::getCon());
    }

    function executaQuery($query) {
        return mysql_query($query, self::getCon());
    }

    function executaQueryAll($query) {
        $posts = array();
        $result = mysql_query($query, self::getCon());

        if (mysql_num_rows($result)) {
            while ($post = mysql_fetch_assoc($result)) {
                $posts[] = $post;
            }
        }

        return $posts;
    }

    function executaQueryRow($query) {
        $result = mysql_query($query, self::getCon());
        return mysql_fetch_assoc($result);
    }

    function gravaRodada($nome, $id_fase) {
        $query = "INSERT INTO tb_rodada (descricao,id_fase) VALUES('" . $nome . "','" . $id_fase . "')";
        if (self::executaQuery($query)) {
            $result = mysql_insert_id();
        } else {
            $result = false;
        }
        return $result;
    }

    function alteraRodada($id, $nome, $id_fase) {
        $query = "UPDATE tb_rodada SET descricao='" . $nome . "',id_fase='" . $id_fase . "' WHERE id_rodada=" . $id;
        if (self::executaQuery($query)) {
            $result = $id;
        } else {
            $result = false;
        }
        return $result;
    }

    function getRodadaById($id_rodada) {
        $query = "SELECT id_rodada,descricao FROM tb_rodada WHERE id_rodada=" . $id_rodada;
        $result = self::executaQueryRow($query);
        return $result;
    }

    function getIdRodadaByNome($nome) {
        $query = "SELECT COUNT(id_rodada) as total FROM tb_rodada WHERE descricao='" . $nome . "'";
        $result = self::executaQueryRow($query);
        return $result['total'];
    }

    function listaAllRodadas() {
        $query = "SELECT id_rodada,descricao FROM tb_rodada WHERE status='S' ORDER BY id_rodada";
        $result = self::executaQueryAll($query);
        return $result;
    }

}
